import {batchDelete, deleteById1, findAll, productfindPage} from '@/api/goods/product';
import {brandFindAll}from '@/api/goods/brand';
import {findlistColour}from '@/api/goods/colour'
import {findlistType,getTree}from '@/api/goods/type';
import pickerOptions from '@/utils/date';
// import {base64} from "@/utils/base64Utils";
import showMessageBox from '@/utils/showMessageBox';
import typename from "../goods/goodstypename";

export default {
    data() {
        return {
            pickerOptions,
            tableData:[],
            id:0,
            total:0,
            batchIds:[],
            searchParams:{currentPage:1,pageSize:5,brandId:"",typeId:"",colourId:""},
            dialogFormVisible:false,
            formData:{},
            brandList:{},
            typeList:{},
            colourList:{},
            options:typename.options,
            goodstypename:[],//级联的值
            type_options:[],

        };
    },
    methods:{
        async getTerrs(){
            this.type_options = await getTree();

        }
        ,
        handleChange(typeList){
            console.log(typeList.length);
            this.searchParams.typeId = typeList[typeList.length-1];
        },
        async findById(row){
            console.log(row)
            this.formData = row;
            this.imageUrl = this.formData.goodsImg;
        },
        selectChange(brandList1){
            if (brandList1.length == 1){
                this.formData.id = brandList1[0].id;
            }
            this.batchIds = brandList1.map(item=>item.id);
        },

        showMsg(){
            showMessageBox.smb('此操作将永久删除该文件, 是否继续?', '删除成功!', '已取消删除').then(r  =>{
                r ? this.batchDeleteByIds():null
            })
        },
        selectAll(){
            findAll().then(response=>{
                this.tableData =response;
            })
        },


        //async 表示searchPage 是异步的，但是 await 表示 要等到 findPage 执行后，代码才会继续执行
        //分页查条件查询
        async searchPage(){
            // this.searchParams.typeId = this.goodstypename[this.goodstypename.length-1];
            let response = await productfindPage(this.searchParams);
                this.tableData = response.data;
                this.total=response.total;


        },
        //分页默认值
        resetForm(){
            this.searchParams = {currentPage: 1,pageSize: 5}
            this.pickerOptions.startDate='';
            this.searchPage();
        },
        currentPageChange(currentIndex){
            this.searchParams.currentPage=currentIndex;
            this.searchPage();
        },
        async deleteById(row){
            console.log(row.id);
            await deleteById1(row.id);
            await this.searchPage();
        },
        async batchDeleteByIds() {
            await batchDelete(this.batchIds);
            this.searchPage();
        },
        //查询品牌
        async findbrandList(){
            console.log("aaa");
            let response = await brandFindAll();
                this.brandList = response;
        },
        //查询颜色
        async findcolourList(){
            let response = await findlistColour();
            console.log(response);
            this.colourList = response;
        },
        //查询类型
        async findtypeList(){
            let response = await findlistType();
                this.typeList = response;
        }
    },
    created(){
        this.searchPage();
        this.findcolourList();
        this.findbrandList();
        this.findtypeList();
        this.getTerrs();
    }
};
